package com.yutel.phone.binh.data.xmlhandler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.yutel.phone.binh.data.vo.VideoDetail;

public class SAXforVideoInfoHandler extends DefaultHandler {
	private ArrayList<String> catorynames;
	private ArrayList<String> subcategoryes;
	private ArrayList<String> alias;
	private ArrayList<String> actores;
	private ArrayList<String> director;
	private VideoDetail vedioInfo;
	private String perTag;// 变量标签
	private StringBuffer sb;

	public VideoDetail getVedioInfo() {
		return vedioInfo;
	}

	public void setVedioInfo(VideoDetail vedioInfo) {
		this.vedioInfo = vedioInfo;
	}

	/**
	 * XML文档开始
	 */
	public void startDocument() throws SAXException {
		vedioInfo = new VideoDetail();
		sb = new StringBuffer();
	}

	/**
	 * 开始读取内容
	 */
	public void startElement(String uri, String localname, String qname,
			Attributes attributes) throws SAXException {
		sb.setLength(0);
		if ("video".equals(localname)) {
			subcategoryes = new ArrayList<String>();
			alias = new ArrayList<String>();
			actores = new ArrayList<String>();
			director = new ArrayList<String>();
			catorynames = new ArrayList<String>();
			vedioInfo.setVid(attributes.getValue("id"));
			vedioInfo.setPlayvideoid(attributes.getValue("playVideoId"));
			vedioInfo.setName(attributes.getValue("name"));
			vedioInfo.setDate(attributes.getValue("date"));
			vedioInfo.setDelete(attributes.getValue("deleted"));
			vedioInfo.setVersion(attributes.getValue("version"));
			vedioInfo.setLastModification(attributes
					.getValue("lastModification"));
			vedioInfo.setPremiereDate(attributes.getValue("premiereDate"));
			vedioInfo.setPoster(attributes.getValue("poster"));
			vedioInfo.setArea(attributes.getValue("area"));
			vedioInfo.setFold(attributes.getValue("fold"));
			vedioInfo.setProviderName(attributes.getValue("providerName"));
			vedioInfo.setProviderIcon(attributes.getValue("providerIcon"));
			vedioInfo.setScore(attributes.getValue("score"));
			vedioInfo.setTotalNumber(attributes.getValue("totalNumber"));
			vedioInfo.setIsTotal(attributes.getValue("isTotal"));
			vedioInfo.setPresentNumber(attributes.getValue("presentNumber"));
			vedioInfo.setPresentTime(attributes.getValue("presentTime"));
			vedioInfo.setDefaultSource(attributes.getValue("defaultSource"));
			vedioInfo.setQualityLevel(attributes.getValue("qualityLevel"));
		} else if ("category".equals(localname)) {
			vedioInfo.setCategoryName(attributes.getValue("name"));
			catorynames.add(attributes.getValue("name"));
		} else if ("result".equals(localname)) {
			vedioInfo.setResourceVersion(attributes.getValue("version"));
			vedioInfo.setSourceTypePinyin(attributes.getValue("sourceType"));
		}
		perTag = localname;
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		sb.append(ch, start, length);
	}

	public void endElement(String uri, String localName, String qname)
			throws SAXException {

		if ("category".equals(localName)) {
			vedioInfo.setCategory_names(catorynames);
		}
		if ("video".equals(localName) && vedioInfo != null) {
			vedioInfo.setActores(actores);
			vedioInfo.setAlias(alias);
			vedioInfo.setDirector(director);
			subcategoryes = null;
			alias = null;
			actores = null;
			director = null;
		}
		if ("subcategory".equals(perTag)) {
			subcategoryes.add(vedioInfo.getCategoryName() + ","
					+ sb.toString());
		} else if ("alias".equals(perTag)) {
			alias.add(sb.toString());
		} else if ("actor".equals(perTag)) {
			actores.add(sb.toString());
		} else if ("director".equals(perTag)) {
			director.add(sb.toString());
		} else if ("summer".equals(perTag)) {
			vedioInfo.setSummer(sb.toString());
		}
		perTag = null;
	}
}